{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "97db0caa-bb65-49cf-acb7-34821a8b9e7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "require 'httparty'\n",
    "require 'dotenv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9ce19b3c-4f63-475a-8718-dc635a76be79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":submit_answer"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class AoCClient\n",
    "    include HTTParty\n",
    "    base_uri 'https://adventofcode.com'\n",
    "\n",
    "    def initialize(year, day, session)\n",
    "        @year = year\n",
    "        @day = day\n",
    "        @options = { headers: { cookie: \"session=#{session}\" } }\n",
    "    end\n",
    "\n",
    "    def get_input()\n",
    "        self.class.get(\"/#{@year}/day/#{@day}/input\", @options)\n",
    "    end\n",
    "\n",
    "    def submit_answer(level, answer)\n",
    "        self.class.post(\"/#{@year}/day/#{@day}/answer\", {**@options, body: {\n",
    "            level: level,\n",
    "            answer: answer,\n",
    "        }})\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2b279352-48e9-4102-b447-95ba22e132bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "year = 2021\n",
    "day = 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cefef45c-5634-47a9-98c4-5085ab9fd4f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Input fetched from adventofcode.com\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "session = Dotenv.load(\"../../.env\")[\"AOC_SESSION\"]\n",
    "client = AoCClient.new(year, day, session)\n",
    "input = client.get_input()\n",
    "\"Input fetched from adventofcode.com\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9ad0d65b-11e2-4e95-9619-11b6a812cbfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1\\n\\n22 13 17 11  0\\n 8  2 23  4 24\\n21  9 14 16  7\\n 6 10  3 18  5\\n 1 12 20 15 19\\n\\n 3 15  0  2 22\\n 9 18 13 17  5\\n19  8  7 25 23\\n20 11 10 24  4\\n14 21 16 12  6\\n\\n14 21 17 24  4\\n10 16 15  9 19\\n18  8 23 26 20\\n22 11 13  6  5\\n 2  0 12  3  7\""
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample = \"7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1\n",
    "\n",
    "22 13 17 11  0\n",
    " 8  2 23  4 24\n",
    "21  9 14 16  7\n",
    " 6 10  3 18  5\n",
    " 1 12 20 15 19\n",
    "\n",
    " 3 15  0  2 22\n",
    " 9 18 13 17  5\n",
    "19  8  7 25 23\n",
    "20 11 10 24  4\n",
    "14 21 16 12  6\n",
    "\n",
    "14 21 17 24  4\n",
    "10 16 15  9 19\n",
    "18  8 23 26 20\n",
    "22 11 13  6  5\n",
    " 2  0 12  3  7\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "429c8fd4-9a14-4b21-85ed-62293dbabe2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":part_one"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def part_one(input)\n",
    "    lines = input.split(\"\\n\").filter {|x| x.length > 0}\n",
    "    nums = lines[0].split(\",\").map {|x| x.to_i()}\n",
    "    n = lines.length / 5\n",
    "    boards = (0..n - 1).map { |i|\n",
    "        ((i * 5 + 1)..(i * 5 + 5)).map { |i|\n",
    "            lines[i]\n",
    "                .split(\" \")\n",
    "                .filter {|x| x.length > 0}\n",
    "                .map {|x| x.to_i()}\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    s = Set[]\n",
    "    for num in nums\n",
    "        s.add(num)\n",
    "        for i in 0..n - 1\n",
    "            for r in 0..4\n",
    "                if boards[i][r].all? { |x| s.include?(x) }\n",
    "                    rem = (0..24)\n",
    "                        .filter { |id| !s.include?(boards[i][id / 5][id % 5]) }\n",
    "                        .map { |id| boards[i][id / 5][id % 5] }\n",
    "                        .sum\n",
    "                    return num * rem\n",
    "                end\n",
    "            end\n",
    "            \n",
    "            for c in 0..4\n",
    "                if (0..4).all? { |r| s.include?(boards[i][r][c]) }\n",
    "                    rem = (0..24)\n",
    "                        .filter { |id| !s.include?(boards[i][id / 5][id % 5]) }\n",
    "                        .map { |id| boards[i][id / 5][id % 5] }\n",
    "                        .sum\n",
    "                    return num * rem\n",
    "                end\n",
    "            end\n",
    "        end\n",
    "    end\n",
    "    \n",
    "    -1\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "43a7a3bd-38b9-4cfb-a863-8f9692800b16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4512"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part_one(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "798c1cf0-15de-40ab-8e35-d0faa9b8c7ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49860"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part_one_ans = part_one(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac58032c-dec9-4117-8d87-622204300c6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "client.submit_answer(1, part_one_ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "20bd2fa3-e774-43d2-bd33-ff62ad7a022a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":part_two"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def part_two(input)\n",
    "    lines = input.split(\"\\n\").filter {|x| x.length > 0}\n",
    "    nums = lines[0].split(\",\").map {|x| x.to_i()}\n",
    "    n = lines.length / 5\n",
    "    boards = (0..n - 1).map { |i|\n",
    "        ((i * 5 + 1)..(i * 5 + 5)).map { |i|\n",
    "            lines[i]\n",
    "                .split(\" \")\n",
    "                .filter {|x| x.length > 0}\n",
    "                .map {|x| x.to_i()}\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    s = Set[]\n",
    "    t = Set.new((0..n - 1).to_a)\n",
    "    for num in nums\n",
    "        s.add(num)\n",
    "        to_del = Set[]\n",
    "        for i in t\n",
    "            for r in 0..4\n",
    "                if boards[i][r].all? { |x| s.include?(x) }\n",
    "                    rem = (0..24)\n",
    "                        .filter { |id| !s.include?(boards[i][id / 5][id % 5]) }\n",
    "                        .map { |id| boards[i][id / 5][id % 5] }\n",
    "                        .sum\n",
    "                    if t.length == 1\n",
    "                        return num * rem\n",
    "                    end\n",
    "                    \n",
    "                    to_del.add(i)\n",
    "                end\n",
    "            end\n",
    "            \n",
    "            for c in 0..4\n",
    "                if (0..4).all? { |r| s.include?(boards[i][r][c]) }\n",
    "                    rem = (0..24)\n",
    "                        .filter { |id| !s.include?(boards[i][id / 5][id % 5]) }\n",
    "                        .map { |id| boards[i][id / 5][id % 5] }\n",
    "                        .sum\n",
    "                    if t.length == 1\n",
    "                        return num * rem\n",
    "                    end\n",
    "                    \n",
    "                    to_del.add(i)\n",
    "                end\n",
    "            end\n",
    "        end\n",
    "        \n",
    "        t.subtract(to_del)\n",
    "    end\n",
    "    \n",
    "    -1\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "62b8aca8-7f06-49ed-824a-a32631e18156",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1924"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part_two(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fb60cee4-6cd7-47b6-a400-9ed6abea2e2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24628"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part_two_ans = part_two(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3802cc9-1332-4a78-aea5-8b811ac4e035",
   "metadata": {},
   "outputs": [],
   "source": [
    "client.submit_answer(2, part_two_ans)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.7.0",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
